package com.kvw.filesystem.core.local;

import com.kvw.filesystem.config.LocalStorageConfig;
import com.kvw.filesystem.core.AbstractStorage;
import com.kvw.filesystem.core.S3Storage;
import com.kvw.filesystem.exception.FileSystemException;
import com.kvw.filesystem.model.FileObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.InputStream;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Desc:
 *
 * @author: keen
 * Date: 2020-08-02
 * Time: 23:32
 */
@Slf4j
public class LocalStorage extends S3Storage {

    private LocalStorageConfig localStorageConfig;

    public LocalStorage(String baseDir) {
        this(new LocalStorageConfig(baseDir));
    }

    public LocalStorage(LocalStorageConfig localStorageConfig) {
        this.localStorageConfig = localStorageConfig;
        this.createBaseDir();
    }

    /**
     * 上传文件
     *
     * @param fileObject
     * @return
     */
    @Override
    public String upload(FileObject fileObject) throws FileSystemException {
        return null;
    }

    /**
     * 下载文件
     *
     * @param fileObject
     * @return
     */
    @Override
    public InputStream download(FileObject fileObject) throws FileSystemException {
        return null;
    }

    /**
     * 删除文件
     *
     * @param fileObject
     * @return
     */
    @Override
    public boolean delete(FileObject fileObject) throws FileSystemException {
        return false;
    }


    @Override
    public void makeBucket(String bucketName) throws FileSystemException {

    }

    @Override
    public List<String> listBuckets() throws FileSystemException {
        return null;
    }

    @Override
    public boolean bucketExists(String bucketName) throws FileSystemException {
        return false;
    }

    @Override
    public void removeBucket(String bucketName) throws FileSystemException {

    }

    private void createBaseDir() {
        String baseDir = this.localStorageConfig.getBaseDir();
        if (StringUtils.isBlank(baseDir)) {
            throw new IllegalArgumentException("baseDir不能为空");
        }
        File dir = new File(baseDir);
        if (!dir.exists()) {
            if (dir.mkdirs()) {
                throw new RuntimeException("创建目录" + baseDir + "失败");
            }
        }
    }
}
